<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>生产配件检验</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
	<style type="text/css">
		.layui-form-item {
			display: inline-block;
			width: 30%;
		}
		thead .layui-table-cell{
		   	height:auto;
		  	overflow:visible;
		  	text-overflow:inherit;
		 	white-space:normal;
		 	text-align: center;
		}
		table th {
			width: 16%
		}
	</style>
</head>
<body>
<!-- 顶部搜索：一个条码框，一个按钮（可无） -->
<div class="" style="margin:10px 0 0 15px;width: 98%">
    <!-- <div class="layui-inline">
        <input style="width: 350px;height: 38px;font-size: 18px;padding-right: 40px;" value="" class="layui-input" id="code" placeholder="条形码" autocomplete="off">
    </div>
    &emsp;&emsp;&emsp;&emsp; -->
    <div class="layui-inline">
        <input class="layui-input" style="font-size: 18px;width: 350px" name="keyword" id="keyword"  placeholder="点击查看待检列表" autocomplete="off">
    </div>
    <button class="layui-btn layui-btn-blue" onclick="search()" style="margin: 0 0 0 10px;"><i class="layui-icon layui-icon-search">  搜索</i></button>
	<input class="layui-btn layui-btn-blue" type="button" style="padding: 0 30px;float:right" onclick = "checkTestLog()" value="查看检验记录">
</div>
<!-- 疑似不良品信息 及操作 -->
<div id="details" style="margin:10px 0 0 0;display:none;background-color:rgb(242,242,242);border-top:#e6e6e6 1px solid">
    <div>
    <input type="text" hidden id="iId"/>
    	<table class="layui-table">
    		<thead>
    			<tr>
    				<th colspan="4"><h2 style="text-align: center;">报检单信息</h2></th>
    			</tr>
    		</thead>
    		<tbody>
    			<tr>
    				<td width="20%">报检单号</td>
    				<td colspan="3"><input readonly class="layui-input" id="iNumber"/></td>
    			</tr>
    			<tr>
    				<td width="20%">配件条码</td>
    				<td><input class="layui-input" readonly id="iCode"/></td>
    				<td width="20%">配件批次号</td>
    				<td><input class="layui-input" readonly id="iBatch"/></td>
    			</tr>
    			<tr>
    				<td width="20%">配件名称</td>
    				<td><input class="layui-input" readonly id="iName"/></td>
    				<td width="20%">配件规格</td>
    				<td><input class="layui-input" readonly id="iStandard"/></td>
    			</tr>
    			<tr>
    				<td width="20%">配件数目</td>
    				<td><input class="layui-input" readonly id="iNum"/></td>
    				<td width="20%">工位</td>
    				<td><input class="layui-input" readonly id="iStation"/></td>
    			</tr>
    			<tr>
    				<td width="20%">报检人</td>
    				<td><input class="layui-input" readonly id="iPerson"/></td>
    				<td width="20%">报检时间</td>
    				<td><input class="layui-input" readonly id="iDate"/></td>
    			</tr>
    			<tr>
    				<td width="20%">异常描述</td>
    				<td colspan="3"><textarea id="iDescription" readonly class="layui-textarea"></textarea></td>
    			</tr>
    		</tbody>
    	</table>
    	<table class="layui-table">
    		<thead>
    			<tr>
    				<th colspan="11"><h2 style="text-align: center;">生产配件检验</h2></th>
    			</tr>
    			<tr>
	   				<th style="width: 5%"></th>
	   				<th style="width: 12%">检测指标</th>
	   				<th style="width: 12%">上限值</th>
	   				<th style="width: 12%">下限值</th>
	   				<th style="width: 12%">检测值</th>
	   				<th style="width: 12%">检测指标数量</th>
	   				<th style="width: 12%">检测指标合格数量</th>
	   				<th style="width: 12%">检测指标不合格数量</th>
	   				<th style="width: 12%">检测结果</th>
	   			</tr>
    		</thead>
    		<tbody id="tbody">
    		
    		</tbody>
    	</table>
    	<form class="layui-form layui-card-body">
		   	<input class="layui-hide" id="tTestPerson">
		   	<input class="layui-hide" id="tTestDate">
		   	<div style="display:inline-block;width:300px">
			   	<select class="layui-select" id="tWay">
			   		<option value="报废">报废</option>
			   		<option value="让步接收">让步接收</option>
			   		<option value="返工">返工</option>
			   	</select>
		   	</div>
	   		<div class="layui-form-item">
		   		<div class="" style="margin:10px 0 0 20px">
			  		<input id="subBtn1" class="layui-btn layui-btn-blue" type="button" style="padding: 0 30px" onclick = "save()" value="提交">
				</div>
			</div>
	   	</form>
    </div>
</div>
<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>
<script type="text/javascript">
	function date2String(timestamp){
		var d = new Date(timestamp);
		var date = (d.getFullYear()) + "-" + 
	       (d.getMonth() + 1<10?"0"+(d.getMonth() + 1):d.getMonth() + 1) + "-" +
	       (d.getDate()<10?"0"+d.getDate():d.getDate()) + " " + 
	       (d.getHours()<10?"0"+d.getHours():d.getHours()) + ":" + 
	       (d.getMinutes()<10?"0"+d.getMinutes():d.getMinutes()) + ":" + 
	       (d.getSeconds()<10?"0"+d.getSeconds():d.getSeconds());
	       
	       return date;
	}
	$(function(){
		$("#keyword").click(function(){
			layer.open({
	            type: 1 					//Page层类型
	            ,area: ['750px', '450px']   //宽  高
	            ,title: '待检列表'
	            ,shade: 0.6 				//遮罩透明度
	            ,maxmin: true			    //允许全屏最小化
	            ,anim: 1 					//0-6的动画形式，-1不开启
	            /* ,content: $("#lookupTable2") */
	            ,content: '<table class="layui-hide" id="lookupTable2" lay-filter="lookupTable2"></table>'
	            ,success: function(){
	                layui.use(['table'], function(){
	                    var table = layui.table;
	                    table.render({
	                        elem: '#lookupTable2'
	                        ,url:'${pageContext.request.contextPath }/test/selectInspectionList.do'
	                        ,title: '待检列表'
	                        ,cols: [[
	                            {field:'', title:'序号', sort: true, type:'numbers', width: 60}
	                            ,{field:'iNumber', title:'报检单号'}
	                            ,{field:'iName', title:'名称'}
	                            ,{field:'iNum', title:'数量'}
	                            ,{field:'iPerson', title:'报检人'}
	                            ,{field:'iDate', title:'报检时间',templet :function (row){
	                                	return date2String(row.iDate.time);
	                            	}   
	                            }
	                        ]]
	                        ,page: false
	                    });
		                table.on('rowDouble(lookupTable2)', function(obj){
		                    var number = obj.data.iNumber;
		                    $("#keyword").val(number);
		                    layer.closeAll();
		                 });
	                });
	            }
	        });
		})
	})

	function search(){
		var code = $("#code").val();
		var keyword = $("#keyword").val();
		$.ajax({
			url:'${pageContext.request.contextPath }/test/selectInspection.do'
			,type:'post'
			,data:{"code":code,"keyword":keyword}
			,dataType:'json'
			,success:function(res){
				if(res==0){
					toastr.warning("没有匹配项！");
					$("#details").css("display","none");
				}else{
					console.log(date2String(res.iDate.time))
					$("#details").css("display","block");
					$("#iId").val(res.iId);
					$("#iCode").val(res.iCode);
					$("#iNumber").val(res.iNumber);
					$("#iBatch").val(res.iBatch);
					$("#iName").val(res.iName);
					$("#iStandard").val(res.iStandard);
					$("#iPerson").val(res.iPerson);
					$("#iDate").val(date2String(res.iDate.time));
					$("#iNum").val(res.iNum);
					$("#iStation").val(res.iStation);
					$("#iDescription").val(res.iDescription);
				}
			}
		})
	}
	layui.use(['table','layer','upload','form', 'layedit', 'laydate'], function(){
		var table = layui.table;
		var layer = layui.layer
	    ,form = layui.form
	    ,layedit = layui.layedit
	    ,laydate = layui.laydate;
		//日期
		laydate.render({
		   elem: '#tTestDate'
		   ,type: 'datetime'
		});
	});
	$.ajax({
		url:'${pageContext.request.contextPath }/criterion/selectAccessoriesCriterion.do?'
		,type:'post'
		,data: {cType:"生产配件检验"}
		,dataType:'json'
		,success:function(data){
			$("#tbody").append(
				'<tr>'+
					'<td align="center"><input name="Fruit" type="checkbox" value="" />'+
					'<td>尺寸1</td>'+
					'<td>'+data.sizeOneUp+'</td>'+
					'<td>'+data.sizeOneDown+'</td>'+
					'<td><input class="layui-input" id="val0" style="width:150px;" step="0.01" type="number" name="tValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tIndexValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tFineValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tBadValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" id="res0"/></td>'+
				'</tr>'+
				'<tr>'+
					'<td align="center"><input name="Fruit" type="checkbox" value="" />'+
					'<td>尺寸2</td>'+
					'<td>'+data.sizeTwoUp+'</td>'+
					'<td>'+data.sizeTwoDown+'</td>'+
					'<td><input class="layui-input" id="val1" style="width:150px;" step="0.01" type="number" name="tValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tIndexValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tFineValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tBadValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" id="res1"/></td>'+
				'</tr>'+
				'<tr>'+
					'<td align="center"><input name="Fruit" type="checkbox" value="" />'+
					'<td>尺寸3</td>'+
					'<td>'+data.sizeThreeUp+'</td>'+
					'<td>'+data.sizeThreeDown+'</td>'+
					'<td><input class="layui-input" id="val2" style="width:150px;" step="0.01" type="number" name="tValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tIndexValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tFineValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" type="number" name="tBadValue"/></td>'+
					'<td><input class="layui-input" style="width:150px;" id="res2"/></td>'+
				'</tr>'
			)
			$("#val0").on('input propertychange', function(){
					var val = Number($(this).val());
					var cUpper = Number(data.sizeOneUp);
					var cLower = Number(data.sizeOneDown);
					if((val <= cUpper) && (val >= cLower)){
						$("#res0").val("合格");
					}else{
						$("#res0").val("不合格");
					}
				})
			$("#val1").on('input propertychange', function(){
					var val = Number($(this).val());
					var cUpper = Number(data.sizeTwoUp);
					var cLower = Number(data.sizeTwoDown);
					if((val <= cUpper) && (val >= cLower)){
						$("#res1").val("合格");
					}else{
						$("#res1").val("不合格");
					}
				})
			$("#val2").on('input propertychange', function(){
					var val = Number($(this).val());
					var cUpper = Number(data.sizeThreeUp);
					var cLower = Number(data.sizeThreeDown);
					if((val <= cUpper) && (val >= cLower)){
						$("#res2").val("合格");
					}else{
						$("#res2").val("不合格");
					}
				})
		}
	})
	//保存质检结果
	function save(){
		$("#subBtn1").attr("disabled",true);
		var iId = $("#iId").val();
		var tiNumber = $("#iNumber").val();
		var tNumber = "ZJ"+(new Date()).getTime();
		var tBatch = $("#iBatch").val();
		var tName = $("#iName").val();
		var tStandard = $("#iStandard").val();
		var tCode = $("#iCode").val();
		var tNum = $("#iNum").val();
		var tStation = $("#iStation").val();
		var tInsPerson = $("#iPerson").val();
		var tInsDate = $("#iDate").val();
		var tWay = $("#tWay").val();
		var tStatus = "0";
		var x = 0;
		var length = $("#tbody tr :checkbox:checked").length;
		if(length==0){
			toastr.warning("请至少选择一条检测数据！");
			$("#subBtn1").attr("disabled",false);
		}else{
			$.ajax({
				type:'post'
				,url:'${pageContext.request.contextPath }/test/insertParetsTest.do'
				,data:{bjNumber:tiNumber,ptNumber:tNumber,paretsBatch:tBatch,paretsName:tName,paretsSpecifications:tStandard,paretsCode:tCode,paretsNum:tNum,station:tStation,insPerson:tInsPerson,insDate:tInsDate}
				,dataType:'json'
				,success:function(res){
					if(res>0){
						$("#tbody tr :checkbox:checked").each(function(i,dom){
							var tr = $(dom).closest("tr");
							var tds = tr.find("td");
							var tIndex = tds[1].innerHTML;
							var upper = tds[2].innerHTML;
							var lower = tds[3].innerHTML;
							var tValue = tds[4].getElementsByTagName("input")[0].value;
							var tIndexValue = tds[5].getElementsByTagName("input")[0].value;
							var tFineValue = tds[6].getElementsByTagName("input")[0].value;
							var tBadValue = tds[7].getElementsByTagName("input")[0].value;
							var tResult = tds[8].getElementsByTagName("input")[0].value;
							if(tResult=="不合格"){
								x+=1;
							}
							$.ajax({
								type:'post'
								,url:'${pageContext.request.contextPath }/test/insertTestDetails.do'
								,data:{tNumber:tNumber,tIndex:tIndex,tStandard:tStandard,upper:upper,lower:lower,tValue:tValue,tIndexValue:tIndexValue,tFineValue:tFineValue,tBadValue:tBadValue,tResult:tResult}
								,dataType:'json'
								,success:function(data){
									
								}
							})
						});
						if(x>0){
							$.ajax({
								type:'post'
								,url:'${pageContext.request.contextPath }/test/insertTest.do'
								,data:{iId:iId,tiNumber:tiNumber,tNumber:tNumber,tBatch:tBatch,tName:tName,tStandard:tStandard,tCode:tCode,tNum:tNum,tStation:tStation,tInsPerson:tInsPerson,tInsDate:tInsDate,tWay:tWay,tStatus:tStatus}
								,dataType:'json'
								,success:function(res){
									
								}
							})
						}
						toastr.success("保存成功！");
						setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							window.location.reload();  
						},2000);
					}else{
						toastr.error("保存失败！");
					}
				}
			})
		}
	}
	function checkTestLog(){
		layui.use(['table','layer','upload','form','laydate'], function(){
			var table = layui.table;
			var layer = layui.layer;
		    var form = layui.form;
		    var laydate = layui.laydate;
		    
			var content = '<div id="hideDv" style="padding:10px 0 0 15px">'+
								'<div class="layui-inline">'+
								'<input class="layui-input" name="checkDate" id="checkDate" placeholder="请选择日期" autocomplete="off" style="display: inline; width: 100%">'+
							'</div>'+
							'<button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">搜索</i></button>'+
							'<table class="layui-hide" id="testLog" lay-filter="testLog"></table>'+
						'</div>'
			layer.open({
		        type: 1 					//Page层类型
		        ,area: ['95%', '600px']   //宽  高
		        ,title: '检测记录'
		        ,shade: 0.6 				//遮罩透明度
		        ,maxmin: true			    //允许全屏最小化
		        ,anim: 1 					//0-6的动画形式，-1不开启
		        /* ,content: $("#lookupTable2") */
		        ,content: content
		        ,success:function(){
		        	laydate.render({
		  			    elem: '#checkDate',
		  			    format: 'yyyy-MM-dd'
		  			  });
		        	var $ = layui.jquery, active = {
		    		    	reload:function () {
		    					var checkDate = $("#checkDate").val();
		    					table.reload('contenttable',{
		    						method:'get',
		    						where:{"keyword":checkDate}
			    					,page: {
			    						curr: 1 //重新从第 1 页开始
			    					}
		    					});
		    				}
		    			}
	    			$('.layui-btn').on('click', function(){
	    		        var type = $(this).data('type');
	    		        active[type] ? active[type].call(this) : '';
	    		    });
	                table.render({
	                    elem: '#testLog'
	                    ,url:'${pageContext.request.contextPath }/test/selectTestLogList.do'
	                    ,title: '检测记录'
	                    ,limits:[10,20,30]
	                    ,id:'contenttable'
	                    ,cols: [[
	                        {field:'', title:'序号', sort: true, type:'numbers', width: 60}
	                        ,{field:'ptNumber', title:'质检单号'}
	                        ,{field:'bjNumber', title:'报检单号'}
	                        ,{field:'paretsCode', title:'配件条码'}
	                        ,{field:'paretsBatch', title:'配件批次'}
	                        ,{field:'paretsName', title:'配件名称'}
	                        ,{field:'paretsSpecifications', title:'配件规格'}
	                        ,{field:'paretsNum', title:'配件数目'}
	                        ,{field:'station', title:'工位'}
	                        ,{field:'insPerson', title:'报检人'}
	                        ,{field:'insDate', title:'报检时间',width:150,templet :function (row){
                            		return date2String(row.insDate);
                        		}   
                       		}
	                        ,{field:'testPerson', title:'检验人'}
	                        ,{field:'testDate', title:'检验时间',width:150,templet :function (row){
	                            	return date2String(row.testDate);
	                        	}   
	                        }
	                    ]]
	                    ,page: true
	                });
		        }
			})
			//监听行单击事件(子表)
			 table.on('row(testLog)', function(obj){
				var ptNumber = obj.data.ptNumber;
				var content = '<div id="hideDv" style="padding:10px 0 0 15px">'+
								'<table class="layui-hide" id="testLogDetails" lay-filter="testLogDetails"></table>'+
							'</div>'
				layer.open({
			        type: 1 					//Page层类型
			        ,area: ['95%', '600px']   //宽  高
			        ,title: '检测记录详情'
			        ,shade: 0.6 				//遮罩透明度
			        ,maxmin: true			    //允许全屏最小化
			        ,anim: 1 					//0-6的动画形式，-1不开启
			        /* ,content: $("#lookupTable2") */
			        ,content: content
			        ,success:function(){
		                table.render({
		                    elem: '#testLogDetails'
		                    ,url:'${pageContext.request.contextPath }/test/selectTestLogDetailsList.do?ptNumber='+ptNumber
		                    ,title: '检测记录详情'
		                    ,limits:[10,20,30]
		                    ,cols: [[
		                        {field:'', title:'序号', sort: true, type:'numbers', width: 60}
		                        ,{field:'tNumber', title:'质检单号'}
		                        ,{field:'tIndex', title:'检测项'}
		                        ,{field:'tStandard', title:'配件规格'}
		                        ,{field:'upper', title:'上限值'}
		                        ,{field:'lower', title:'下限值'}
		                        ,{field:'tValue', title:'检测值'}
		                        ,{field:'tIndexValue', title:'检测指标数量'}
		                        ,{field:'tFineValue', title:'检测合格数量'}
		                        ,{field:'tBadValue', title:'检测不合格数量'}
		                        ,{field:'tResult', title:'检验结果'}
		                    ]]
		                    ,page: true
		                });
			        }
				})
			});
		})
	}
	toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>
</body>
</html>